var templateParent = '<ul class="listRow row{0}" onclick="reshow(\'{3}\', this)"><li class="li1 b-left" title="{1}">{1}</li><li class="double li2">&nbsp;</li><li class="tRight money li3"><span class="money-info">{2}</span></li></ul>';
var templateChild = '<ul class="listRowC rowC r{2} double{4} {5}" style="display:none;" onclick="go({3});"><li class="li1">&nbsp;</li><li class="double li2"><span class="second-category-lable">{0}</span></li><li class="tRight money li3"><span class="money-info">{1}</span></li></ul>';
var nodata = "<div class='nodata'><div class='box'><ul><li></li><li class='row1'></li><li></li><li class='row1'></li><li></li></ul><div class='bg png'>&nbsp;</div></div></div>";
var demo = "<div class='demo' style='top: -4px;'></div>";
var demo2 = "<div class='demo' style='top: -5px;'></div>";
var bottomShadow = '<div class="bottomShadow rowC r{0}" style="display:none;"></div>';
var gopara = "";
var d_N = 2;
var min_rows = 6
    , dateFormat = "yyyy.MM.dd";
var highchartColor = {
    columnCharColor: ["#d82525", "#25d866", "#409EFF", "#f7b04b", "#b1e165", "#89c452", "#38b470", "#35d5d6", "#41b5dd", "#7c90f1", "#697be6", "#79408e", "#934b8c", "#ad5dbe", "#e258a7"],
    pieCharColor: ["#d82525", "#25d866", "#409EFF", "#ffd629", "#a7dd51", "#7abd3b", "#1eaa5d", "#1ecfd1", "#2aacd8", "#6a82ef", "#4c60df", "#672780", "#85347c", "#9633ac", "#f42a9d"]
};
String.prototype.format = function() {
    if (!arguments) {
        return this
    }
    if (arguments.length == 1) {
        return this.replace(new RegExp("\\{0\\}","g"), arguments[0])
    }
    var tmp = this;
    for (var i = 0; i < arguments.length; i++) {
        tmp = tmp.replace(new RegExp("\\{" + i + "\\}","g"), arguments[i])
    }
    return tmp
}
Number.prototype.format = function(digits) {
    d = digits ? digits : 0;
    if (this % 1 == 0) {
        return this.toFixed(0)
    } else {
        return this.toFixed(d)
    }
}
function formatMoney(s, n) {
    n = n >= 0 && n <= 20 ? n : 2;
    s = parseFloat((s + "").replace(/[^\d\.-]/g, ""));
    prev = "";
    if (s < 0) {
        prev = "-";
        s = 0 - s
    }
    s = s.toFixed(n) + "";
    var l = s.split(".")[0].split("").reverse()
        , r = s.split(".")[1];
    t = "";
    for (var i = 0; i < l.length; i++) {
        t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "")
    }
    return prev + t.split("").reverse().join("") + (n > 0 ? "." + r : "")
}
function genDailyGraphFromData(data) {
    data.symbol = ''
    var arrChartCategory = []
    var arrChart = []
    if (data && data.inlst && data.inlst.length) {
        var count = data.inlst.length;
        var arrChartCategory = new Array();
        var arrChart = new Array();
        for (var i = 0; i < count; i++) {
            var _t = data.inlst[i];
            arrChartCategory.push(_t.name);
            var last_child = "";
            for (var k = 0; k < _t.c.length; k++) {
                var _tmp = _t.c[k];
                if (k == (_t.c.length - 1)) {
                    last_child = "last-listRow-child"
                }
                arrChart.push(getYAxisData(_tmp.name, _tmp.amount, i, count, 0));
                _tmp = null
            }
            _t = null
        }
    }
    renderChart("statDailyIn", arrChartCategory, arrChart, data.maxI,"收入堆积图")
    // renderChart("statDailyIn", demoDataIn.dataCategory, demoDataIn.datas, demoDataIn.maxV, "收入堆积图");
    // 支出报表
    var arrChartCategoryOut = [];
    var arrChartOut = [];
    if (data && data.outlist && data.outlist.length) {
        var arrChartOut = new Array();
        var arrChartCategoryOut = new Array();
        var count = data.outlist.length;
        for (var i = 0; i < count; i++) {
            var _t = data.outlist[i];
            arrChartCategoryOut.push(_t.name);
            var last_child = "";
            for (var k = 0; k < _t.c.length; k++) {
                var _tmp = _t.c[k];
                if (k == (_t.c.length - 1)) {
                    last_child = "last-listRow-child"
                }
                arrChartOut.push(getYAxisData(_tmp.name, _tmp.amount, i, count));
                _tmp = null
            }
            _t = null
        }
    }
    renderChart("statDailyOut", arrChartCategoryOut, arrChartOut, data.maxO, "支出堆积图")
    // renderChart("statDailyOut", demoDataOut.dataCategory, demoDataOut.datas, demoDataOut.maxV, "支出堆积图");
}
function renderPieChartTwo(id, datas,width=360,height=250)  {
    var chart = new Highcharts.Chart({
        chart: {
            renderTo: id,
            margin: [0, 0, 0, 0],
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false,
        },
        colors: highchartColor.pieCharColor,
        title: {
            text: "",
        },
        tooltip: {
            borderRadius: 4,
            style: {
                color: "#2e2e2e",
                cursor: "default",
                fontSize: "12px",
                padding: "8px",
                lineHeight: "20px",
                fontFamily: "微软雅黑"
            },
            shadow: false,
            formatter: function() {
                return "<b>" + this.point.name + "</b>: " + this.y.format(1) + " %"
            }
        },
        legend: {
            layout: "vertical",
            borderWidth: 0,
            verticalAlign: "right",
            floating: true,
            itemWidth: 80,
            itemStyle: {
                color: "#6c6c6c",
                cursor: "pointer",
                fontSize: "12px",
                fontWeight: "normal"
            },
            x: 100,
            y: 10,
            labelFormatter: function() {
                return this.name
            }
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                innerSize:"85%",
                center: ["35%", "50%"],
                dataLabels: {
                    enabled: true,
                    distance: -36,
                    formatter: function() {
                        if (this.y > 6) {
                            return this.percentage.toFixed(2) + "%"
                        }
                    },
                    color: "#fff",
                    style: {
                        fontFamily: "微软雅黑",
                        fontWeight: "normal",
                        textShadow: "none"
                    }
                },
                showInLegend: true
            }
        },
        series: [{
            type: "pie",
            name: "Browser share",
            data: datas
        }],
        credits: {
            enabled: false
        }
    })
}
function renderCategoryProject(data,totalin){
    let arrChart = new Array();

    for (var i = 0; i < data.length; i++) {
        var _t = data[i];
        if (_t) {
            var _c = _t.total < 0 ? "red" : "";
            if (_t.amounts > 0) {
                if (totalin != 0) {
                    var percent = parseFloat((_t.amounts * 100 / totalin))
                } else {
                    var percent = parseFloat(0)
                }
                var isEmpty = false;
                arrChart.push(packData(_t.category_parent_str, percent));
            }
            _t = null
        }
    }


    console.log(arrChart)

    renderPieChartTwo("categoryProject", arrChart);

}
function renderZhuReport(id,xDatas, datas, type='column'){
    console.log(datas)
    var chart = new Highcharts.Chart({
        chart: {
            renderTo: id,
            type: type
        },
        colors: ['#FFB444','#FFE0B2','#FFC93F'],
        title: {
            text: ''
        },
        xAxis: {
            categories: xDatas
        },
        yAxis: {
            min: 0,
            title: {
                text: ''
            }
        },
        series: datas
    })

}
function genMemberGraphFromData(data) {
    var arrChart = new Array();
    var min_item = 5;
    var max = (data.inlst.length > data.outlist.length) ? data.inlst.length : data.outlist.length;
    var count = max > min_item ? max : min_item;
    var totalin = 0;
    for (var i = 0; i < count; i++) {
        var _t = data.inlst[i];
        if (_t) {
            if (_t.total > 0) {
                totalin += parseFloat(_t.total)
            }
            _t = null
        }
    }
    for (var i = 0; i < count; i++) {
        var _t = data.inlst[i];
        if (_t) {
            var _c = _t.total < 0 ? "red" : "";
            if (_t.total > 0) {
                if (totalin != 0) {
                    var percent = parseFloat((_t.total * 100 / totalin))
                } else {
                    var percent = parseFloat(0)
                }
                var isEmpty = false;
                arrChart.push(packData(_t.name, percent));
            }
            _t = null
        }
    }
    renderPieChart("statMemberIn", arrChart);
    arr = new Array();
    arrChart = new Array();
    isEmpty = true;
    var totalout = 0;
    for (var i = 0; i < count; i++) {
        var _t = data.outlist[i];
        if (_t) {
            if (_t.total > 0) {
                totalout += parseFloat(_t.total)
            }
            _t = null
        }
    }
    for (var i = 0; i < count; i++) {
        var _t = data.outlist[i];
        if (_t) {
            var _c = _t.total < 0 ? "red" : "";
            if (_t.total > 0) {
                if (totalout != 0) {
                    var percent = parseFloat((_t.total * 100 / totalout))
                } else {
                    var percent = parseFloat(0)
                }
                isEmpty = false;
                arrChart.push(packData(_t.name, percent));
            }
            _t = null
        }
    }
    renderPieChart("statMemberOut", arrChart);
}

function genGuaData(data) {
    var arrChart = new Array();
    var min_item = 5;
    var max = data.data.length;
    var count = max > min_item ? max : min_item;
    var totalin = 0;
    for (var i = 0; i < count; i++) {
        var _t = data.data[i];
        if (_t) {
            if (_t.total > 0) {
                totalin += parseFloat(_t.total)
            }
            _t = null
        }
    }
    for (var i = 0; i < count; i++) {
        var _t = data.data[i];
        if (_t) {
            var _c = _t.total < 0 ? "red" : "";
            if (_t.total > 0) {
                if (totalin != 0) {
                    var percent = parseFloat((_t.total * 100 / totalin))
                } else {
                    var percent = parseFloat(0)
                }
                var isEmpty = false;
                arrChart.push(packData(_t.name, percent));
            }
            _t = null
        }
    }
    renderPieChart("statGua", arrChart,700);
}
function genYuData(data) {
    var arrChart = new Array();
    var min_item = 5;
    var max = data.data.length;
    var count = max > min_item ? max : min_item;
    var totalin = 0;
    for (var i = 0; i < count; i++) {
        var _t = data.data[i];
        if (_t) {
            if (_t.total > 0) {
                totalin += parseFloat(_t.total)
            }
            _t = null
        }
    }
    for (var i = 0; i < count; i++) {
        var _t = data.data[i];
        if (_t) {
            var _c = _t.total < 0 ? "red" : "";
            if (_t.total > 0) {
                if (totalin != 0) {
                    var percent = parseFloat((_t.total * 100 / totalin))
                } else {
                    var percent = parseFloat(0)
                }
                var isEmpty = false;
                arrChart.push(packData(_t.name, percent));
            }
            _t = null
        }
    }
    renderPieChart("statYu", arrChart,700);
}

function genStoreGraphFromData(data) {
    var arrChart = new Array();
    var min_item = 5;
    var max = data.outlist.length;
    var count = max > min_item ? max : min_item;
    var totalin = 0;
    // for (var i = 0; i < count; i++) {
    //     var _t = data.inlst[i];
    //     if (_t) {
    //         if (_t.total > 0) {
    //             totalin += parseFloat(_t.total)
    //         }
    //         _t = null
    //     }
    // }
    // for (var i = 0; i < count; i++) {
    //     var _t = data.inlst[i];
    //     if (_t) {
    //         var _c = _t.total < 0 ? "red" : "";
    //         if (_t.total > 0) {
    //             if (totalin != 0) {
    //                 var percent = parseFloat((_t.total * 100 / totalin))
    //             } else {
    //                 var percent = parseFloat(0)
    //             }
    //             var isEmpty = false;
    //             arrChart.push(packData(_t.name, percent));
    //         }
    //         _t = null
    //     }
    // }
    // renderPieChart("statMemberIn", arrChart);
    arr = new Array();
    arrChart = new Array();
    isEmpty = true;
    var totalout = 0;
    for (var i = 0; i < count; i++) {
        var _t = data.outlist[i];
        if (_t) {
            if (_t.total > 0) {
                totalout += parseFloat(_t.total)
            }
            _t = null
        }
    }
    for (var i = 0; i < count; i++) {
        var _t = data.outlist[i];
        if (_t) {
            var _c = _t.total < 0 ? "red" : "";
            if (_t.total > 0) {
                if (totalout != 0) {
                    var percent = parseFloat((_t.total * 100 / totalout))
                } else {
                    var percent = parseFloat(0)
                }
                isEmpty = false;
                arrChart.push(packData(_t.name, percent));
            }
            _t = null
        }
    }
    renderPieChart("statStoreOut", arrChart);
}
function packData(name, percent) {
    var obj = new Object();
    obj.name = name;
    obj.y = percent;
    return obj
}
function formatMillionMoney(s, n, m) {
    if (s > 9999999 || s < -9999999) {
        s = s / 100000000;
        return formatMoney(s, m) + "<span class='money-unit'>亿</span>"
    }
    return formatMoney(s, n)
}

var demoDataIn = {
    dataCategory: ["职业收入", "其他收入", "投资收入"],
    datas: [{
        name: "工资收入",
        data: [1500, 0, 0]
    }, {
        name: "意外来财",
        data: [0, 1800, 0]
    }, {
        name: "投资股票",
        data: [0, 0, 2500]
    }],
    maxV: 5500
};
var demoDataOut = {
    dataCategory: ["衣服饰品", "居家物业", "行车交通"],
    datas: [{
        name: "衣服裤子",
        data: [50, 0, 0]
    }, {
        name: "化妆饰品",
        data: [160, 0, 0]
    }, {
        name: "房租",
        data: [0, 300, 0]
    }, {
        name: "公共交通",
        data: [0, 0, 350]
    }, {
        name: "打车租车",
        data: [0, 0, 100]
    }],
    maxV: 1000
};
function getYAxisData(name, amount, index, count) {
    var obj = new Object();
    obj.name = name;
    var data = new Array();
    for (var i = 0; i < count; i++) {
        if (i == index) {
            data.push(parseFloat(amount))
        } else {
            data.push(0)
        }
    }
    obj.data = data;
    return obj
}
function renderChart(id, dataCategory, datas, maxV, title) {
    var chart = new Highcharts.Chart({
        chart: {
            renderTo: id,
            defaultSeriesType: "column",
            // width: 770,
            height: 250
        },
        colors: highchartColor.columnCharColor,
        title: {
            text: title
        },
        xAxis: {
            categories: dataCategory,
            labels: {
                style: {
                    color: "#494c54",
                    fontWeight: "normal",
                    fontFamily: "Microsoft YaHei",
                    fontSize: "14px"
                }
            }
        },
        yAxis: {
            min: 0,
            max: maxV,
            title: {
                text: ""
            }
        },
        legend: {
            enabled: false
        },
        tooltip: {
            style: {
                color: "#2e2e2e",
                cursor: "default",
                fontSize: "12px",
                padding: "8px",
                lineHeight: "20px",
                fontFamily: "微软雅黑"
            },
            shadow: false,
            formatter: function() {
                return "<b>" + this.x + "</b><br/>" + this.series.name + ": " + formatMoney(this.y, d_N) + "<br/>合计: " + formatMoney(this.point.stackTotal, d_N)
            }
        },
        plotOptions: {
            column: {
                stacking: "normal"
            }
        },
        series: datas,
        credits: {
            enabled: false
        }
    })
}
function renderPieChart(id, datas,width=360,height=250)  {
    var chart = new Highcharts.Chart({
        chart: {
            renderTo: id,
            width: width,
            height: height,
            margin: [0, 0, 0, 0],
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        colors: highchartColor.pieCharColor,
        title: {
            text: ""
        },
        tooltip: {
            borderRadius: 4,
            style: {
                color: "#2e2e2e",
                cursor: "default",
                fontSize: "12px",
                padding: "8px",
                lineHeight: "20px",
                fontFamily: "微软雅黑"
            },
            shadow: false,
            formatter: function() {
                return "<b>" + this.point.name + "</b>: " + this.y.format(1) + " %"
            }
        },
        legend: {
            layout: "vertical",
            borderWidth: 0,
            itemWidth: 60,
            verticalAlign: "right",
            floating: true,
            itemStyle: {
                color: "#6c6c6c",
                cursor: "pointer",
                fontSize: "12px",
                fontWeight: "normal"
            },
            x: 100,
            y: 10,
            labelFormatter: function() {
                if (this.name.length > 8) {
                    return this.name.substr(0, 7) + ".."
                } else {
                    return this.name
                }
            }
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                size: "85%",
                center: ["35%", "50%"],
                dataLabels: {
                    enabled: true,
                    distance: -36,
                    formatter: function() {
                        if (this.y > 6) {
                            return this.percentage.toFixed(2) + "%"
                        }
                    },
                    color: "#fff",
                    style: {
                        fontFamily: "微软雅黑",
                        fontWeight: "normal",
                        textShadow: "none"
                    }
                },
                showInLegend: true
            }
        },
        series: [{
            type: "pie",
            name: "Browser share",
            data: datas
        }],
        credits: {
            enabled: false
        }
    })
}
function renderMonthy(id, datas, type='column') {
    var chart = new Highcharts.Chart({
        chart: {
            renderTo: id,
            type: type
        },
        colors: highchartColor.columnCharColor,
        title: {
            text: ''
        },
        xAxis: {
            categories: datas.dataCategory
        },
        yAxis: {
            min: 0,
            title: {
                text: ''
            }
        },
        tooltip: {
        // head + 每个 point + footer 拼接成完整的 table
            headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
            pointFormat: '<tr><td style="color:{series.color};padding:5px">{series.name}: </td>' +
            '<td style="padding:0"><b>{point.y:.2f} </b></td></tr>',
            footerFormat: '</table>',
            shared: true,
            useHTML: true
        },
        series: datas.datas
    })
}


